; 递归
(define (product term a next b)
    (if (> a b )
        1
        (*  (term a)
            (product term (next a) next b))
    )
)

(define (factorial n)
    (define (add_1 x)
        (+ 1 x))
    (product * 1 add_1 n )
)
(display (factorial 5))
(newline)
; 迭代
(define (product-iter term a next b)
    (define (iter n result)
        (if (> n b)
            result            
            (iter   (next n)
                    (*   result
                        (term n)
                    )                 
            )
        )
    )
    (iter a 1)
)
(define (factorial-iter n)
    (define (add_1 x)
        (+ 1 x))
    (product-iter * 1 add_1 n )
)
(display (factorial 5))
(newline)

; calc pi
(define (pi)
    (define (add_2 x)
        (+ 2 x))
    (define (term1 x)
        (/ x (+ 1.0 x))
    )
    (define (term2 x)
        (/ (+ 1 x) x)
    )    
    (*  4
        (product term1 2 add_2 1000)
        (product term2 3 add_2 1000)
    ) 
)
(display (pi))

(exit)